Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed #11202/#11199/#11201/#12330/#12774 / refs #13508/#11200/#13506 - fixed various floating-point comparison regressions #7148

Merged
merged 4 commits into from
Jan 16, 2025

Conversation

firewave
Copy link
Collaborator

No description provided.

@firewave
Copy link
Collaborator Author

This hacks in the previous behavior - see b0b3f7e#r136702783.

The goal here is to at least collect all the test cases and add them in as TODO ones so this can be looked into properly.

@firewave firewave force-pushed the fp-comp-x branch 4 times, most recently from 68e448f to 1329d34 Compare December 31, 2024 15:21
@firewave
Copy link
Collaborator Author

firewave commented Dec 31, 2024

This should be complete now in covering the regressions of the commit in question. @pfultz2 please have a look

The change does not fix all the regressions but greatly improved things. And unfortunately it re-introduces a false positive fixed in https://trac.cppcheck.net/ticket/11203. I have not looked further into this yet.

lib/vf_settokenvalue.cpp Show resolved Hide resolved
@@ -550,14 +551,14 @@ namespace ValueFlow
setTokenValue(parent, std::move(result), settings);
} else if (Token::Match(parent, "%op%")) {
if (Token::Match(parent, "%comp%")) {
if (!result.isFloatValue() && !value1.isIntValue() && !value2.isIntValue())
if (!isFloat && !value1.isIntValue() && !value2.isIntValue())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The !isFloat is redundant here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the same but it is not (this is actually the important change based on my older comment). It reflects if either is a float. That doesn't mean one of them cannot be an Int.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, To make it clearer dont use the isFloat variable, write it completely: if(!value1.isFloatValue() && !value2.isFloatValue() && !value1.isIntValue() && !value2.isIntValue())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well - I took it from the original code.

continue;
} else {
if (value1.isTokValue() || value2.isTokValue())
break;
}
bool error = false;
if (result.isFloatValue()) {
if (isFloat) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, looking at the test, we just need to run calculate as both float if one operand is float and convert to the result type int or float at the end.

Also rather than use lambdas(like intValue1) to do the conversion we should instead update the getIntValue and getFloatValue methods to do the conversion. This should make the code much simpler.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the regression introduced by my change. Thanks.

I will take a look into the remaining false negatives tomorrow. There is also a few test variations I still like to add.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like everything has already been fixed. The existing false negatives do not appear to be related to the commit in question but a much earlier one. Will clean up the tests tomorrow.

@firewave firewave force-pushed the fp-comp-x branch 5 times, most recently from 148ce7f to e5a6758 Compare January 1, 2025 19:33
@firewave
Copy link
Collaborator Author

firewave commented Jan 1, 2025

The tests are clean now but there is some weird behavior that I get a warning on the CLI but not in the tests.

@firewave firewave force-pushed the fp-comp-x branch 2 times, most recently from 95b46f2 to 47cdc07 Compare January 1, 2025 21:47
@firewave
Copy link
Collaborator Author

firewave commented Jan 1, 2025

I think I finally have figured this out. Sorry for all the noise.

This still leaves the https://trac.cppcheck.net/ticket/11200 regression but I guess that can be looked at in a follow-up.

@firewave
Copy link
Collaborator Author

firewave commented Jan 5, 2025

The case in TestValueFlow::valueFlowCalculations() which causes the FPE is

ASSERT_EQUALS(tokenValues(";((-1) * 9223372036854775807LL - 1) % (-1);", "%").size(), 1);

Like the input we end up doing x % -1 in calculate() which is an invalid operation.

The problem is that with these changes x is a double (which seems correct given the input) and thus the error checks no longer work. They look way too specific for a certain case and probably just need to be adjusted.

if (error)
*error = true;
return R{};
}
return wrap(x / y);
case '%':
if (isZero(MathLib::bigint(y)) || (std::is_integral<T>{} && std::is_signed<T>{} && isEqual(y, T(-1)) && isEqual(x, std::numeric_limits<T>::min()))) {
if (isZero(MathLib::bigint(y)) || (std::is_signed<T>{} && MathLib::bigint(y) < 0)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrchr-github This crash fix from #5587 was way too specific.

@firewave firewave changed the title attempt to fix various floating-point comparison regressions fixed #11202/#11199/#11201/#12330/#12774 / refs #13508/#11200/#13506 - fixed various floating-point comparison regressions Jan 5, 2025
@firewave firewave marked this pull request as ready for review January 5, 2025 14:35
@firewave
Copy link
Collaborator Author

firewave commented Jan 7, 2025

@pfultz2 please have another look.

continue;
} else {
if (value1.isTokValue() || value2.isTokValue())
break;
}
bool error = false;
auto val = calculate(parent->str(), floatValue1, floatValue2, &error);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't calculate the integral values using floating-point. If both value1 and value2 are integrals then you should use the intvalue to calculate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it works... 😃

Actually, looking at the test, we just need to run calculate as both float if one operand is float and convert to the result type int or float at the end.

Reading this again I see I got it wrong. So re-introduce the previous calculation code but also keep the assignment code I added. Will do later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted it and it still passed locally. Duplicated some code for now.

@firewave
Copy link
Collaborator Author

@pfultz2 Anything left to do? I would like to get this merged so I can proceed looking into the remaining issues.

@firewave firewave merged commit d777511 into danmar:main Jan 16, 2025
60 checks passed
@firewave firewave deleted the fp-comp-x branch January 16, 2025 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants